package main

import "fmt"

type T = struct{}

// channel for transferring channel
var counter = func(n int) chan<- chan<- int {
	requests := make(chan chan<- int)

	go func() {
		for request := range requests {
			if request == nil {
				n++
			} else {
				request <- n
			}
		}
	}()

	return requests
}(0)

func main() {
	increase1000 := func(done chan<- T) {
		for i := 0; i < 1000; i++ {
			counter <- nil
		}
		done <- T{}
	}

	done := make(chan T)
	go increase1000(done)
	go increase1000(done)

	<-done
	<-done

	request := make(chan int, 1)
	counter <- request

	fmt.Println(<-request)
}
